package org.choerodon.address.controller;

import org.apache.ibatis.annotations.Delete;
import org.choerodon.address.domain.AreaVo;
import org.choerodon.address.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author jiu.yang@hand-china.com
 * @version 1.0
 * @description: TODO
 * @date 2021/11/24 8:35
 */
@RestController("projectController.v1")
@RequestMapping("/v1/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping("/getAddress")
    public String getAddress(HttpServletRequest request, HttpServletResponse response) {
        try {
            return addressService.getAddressInfo(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }


    @PostMapping("/initialization")
    public AreaVo initialization(@RequestBody AreaVo areaVo) {
        try {
            return addressService.initialization(areaVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @PostMapping("/save")
    public List<AreaVo> save(@RequestBody AreaVo areaVo) {
        try {
            return addressService.save(areaVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @GetMapping("/query/{id}")
    public List<AreaVo> query(@PathVariable Long id) {
        try {
            return addressService.queryById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @PostMapping("/add")
    public Boolean add(@RequestBody AreaVo areaVo) {
        try {
            return addressService.add(areaVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Delete("/delete")
    public Boolean delete(@RequestBody AreaVo areaVo) {
        try {
            return addressService.delete(areaVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

}
